//
//  TCNavigate.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/3/16.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

@objc protocol TCNavigateDelegate {
    @objc func userNav_didSelect(index:Int,label:MSLabel)
}
class TCNavigate: NSView {
    
    var subLabs:[MSLabel] = []
    var list:[String] = ["参会者","聊天"]
    var delegate:TCNavigateDelegate?
    var index:Int = 0
    
    var selectIndex:Int = 0 {
        didSet {
            let centerX = (kTRTCUserWidth/CGFloat(self.subLabs.count)-80)/2+kTRTCUserWidth/CGFloat(self.subLabs.count)*CGFloat(selectIndex)
            self.selectLine.snp.updateConstraints { (make) in
                make.left.equalTo(centerX)
            }
        }
    }
    
    convenience init(list:[String],index:Int=0) {
        self.init()
        self.list = list
        self.index = index
        self.setupUI()
        self.loadData()
    }
    
    func loadData()  {
        self.selectIndex = index
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        //self.setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        self.wantsLayer = true
        self.layer?.backgroundColor = color_dark90.cgColor
        
        self.subLabs = self.initBtns(inView: self, kwidth: kTRTCUserWidth)
        
        self.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.centerX.width.equalToSuperview()
            make.height.equalTo(1)
            make.bottom.equalToSuperview().offset(-1)
        }
        
        let centerX = (kTRTCUserWidth/CGFloat(self.subLabs.count)-80)/2
        self.addSubview(selectLine)
        selectLine.snp.makeConstraints { (make) in
            make.left.equalTo(centerX)
            make.width.equalTo(80)
            make.height.equalTo(4)
            make.bottom.equalToSuperview().offset(-1)
        }
        
    }
    
    func initBtns(inView:NSView,kwidth:CGFloat) -> [MSLabel] {
        var subLabs:[MSLabel] = []
        let width = kwidth/CGFloat(self.list.count)
        for (inx,val) in self.list.enumerated() {
            let lab = MSLabel()
            lab.setLabel(title: val, textColor: color_dark160, font:  NSFont.systemFont(ofSize: 15))
            lab.alignment = .center
            lab.tag = inx
            lab.tap = { view in
                self.labSubAction(senber: view as! MSLabel)
            }
            inView.addSubview(lab)
            subLabs.append(lab)
            lab.snp.makeConstraints { (make) in
                make.width.equalTo(width)
                make.height.equalTo(18)
                make.centerY.equalToSuperview()
                if inx == 0 {
                    make.left.equalTo(0)
                }else {
                    make.left.equalTo(subLabs[inx-1].snp.right)
                }
            }
        }
        return subLabs
    }
    
    func labSubAction(senber:MSLabel) {
        if self.delegate != nil {
            self.delegate?.userNav_didSelect(index: senber.tag,label: senber)
        }
    }
    
    lazy var selectLine: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_main108.cgColor
        return view
    }()
    
    
    lazy var lineView: NSView = {
        let view = NSView()
        view.wantsLayer = true
        view.layer?.backgroundColor = color_gray(215).cgColor
        view.isHidden = true
        return view
    }()
    
}
